package com.maiji.cloud.controller.shopingmall;

import com.maiji.cloud.entities.manager.User;
import com.maiji.cloud.entities.shopingmall.CapitalIOEntity;
import com.maiji.cloud.entities.shopingmall.WithdrawApply;
import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.request.shopingmall.CapitalIOListReqData;
import com.maiji.cloud.request.shopingmall.OrderRefundReqData;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.maiji.cloud.controller.BaseController;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.response.shopingmall.CapitalIOListResDto;
import com.maiji.cloud.response.shopingmall.FinancialDetailsResDto;
import com.maiji.cloud.response.shopingmall.IntegralFlowResDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

@Api(tags = {"资金模块"})
@RestController
@RequestMapping("/capital/")
public class CapitalController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 现金流水列表
     * @param param
     * @param maijiToken
     * @return
     */
	@PostMapping("financialDetails")
	@ApiOperation(value = "现金流水列表")
	public FinancialDetailsResDto financialDetails(@RequestBody ReqMetaData param ,@RequestHeader("maijiToken") String maijiToken) {

		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/financialDetails", param, FinancialDetailsResDto.class);
	}
	
	/**
	 * 获得积分流水
	 * 
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("integralFlow")
	@ApiOperation(value = "获得积分流水")
	public IntegralFlowResDto integralFlow(@RequestBody ReqMetaData param, @RequestHeader("maijiToken") String maijiToken) {
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/integralFlow", param, IntegralFlowResDto.class);
	}

	/**
	 * 获得提现记录列表
	 * 
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("getcapitalIOList")
	@ApiOperation(value = "获得提现记录列表")
	public CapitalIOListResDto getcapitalIOList(@RequestBody ReqMetaData param, @RequestHeader("maijiToken") String maijiToken) {
		return postForObjectWithToken(maijiToken,REST_URL_SHOPINGMALL + "/capital/getcapitalIOList", param, CapitalIOListResDto.class);
	}

	/**
	 * 客服审核申请退款
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("agreeApplyRefund")
	@ApiOperation(value = "客服审核申请退款")
	public BaseResDto agreeApplyRefund(@RequestBody BaseDataReqDto<OrderRefundReqData> baseDataReqDto) {
		return restTemplate.postForObject(REST_URL_SHOPINGMALL + "/capital/agreeApplyRefund", baseDataReqDto, BaseResDto.class);
	}

	/**
	 * 退货退款确认收货
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("confirmReceived")
	@ApiOperation(value = "退货退款确认收货")
	public BaseResDto confirmReceived(@RequestBody BaseDataReqDto<OrderRefundReqData> baseDataReqDto) {
		return restTemplate.postForObject(REST_URL_SHOPINGMALL + "/capital/confirmReceived", baseDataReqDto, BaseResDto.class);
	}

	/**
	 * 客服执行退款
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("executeRefund")
	@ApiOperation(value = "客服执行退款")
	public BaseResDto executeRefund(@RequestBody BaseDataReqDto<OrderRefundReqData> baseDataReqDto) {
        String userId = baseDataReqDto.getData().getUserId();
        String executePwd = baseDataReqDto.getData().getExecutePwd();
        User user = userService.selectById(userId);
        if (! new BCryptPasswordEncoder().matches(executePwd, user.getExecutePwd()))
            return new BaseResDto(Status.EXECUTEPWDERROR);
        BaseDataReqDto<String> dataReqDto = new BaseDataReqDto<String>().setData(baseDataReqDto.getData().getUuId());
        return restTemplate.postForObject(REST_URL_SHOPINGMALL + "/capital/executeRefund", dataReqDto, BaseResDto.class);
	}

    /**
     * 获得用户提现记录列表
     * @return
     */
    @PostMapping("findAllWithdrawalRecords")
    @ApiOperation(value = "获得用户提现记录列表")
    public BaseMetaResDto<List<CapitalIOEntity>> findAllWithdrawalRecords(@RequestBody BaseDataReqDto<CapitalIOListReqData> baseDataReqDto) {
        return restTemplate.postForObject(REST_URL_SHOPINGMALL + "/capital/findAllWithdrawalRecords", baseDataReqDto, BaseMetaResDto.class);
    }

    /**
     * 获取分销资金提现申请列表
     * @return
     */
    @PostMapping("findAllWithdrawApplys")
    @ApiOperation(value = "获取分销资金提现申请列表")
    public BaseMetaResDto<List<WithdrawApply>> findAllWithdrawApplys (@RequestBody BaseDataReqDto<WithdrawApply> baseDataReqDto) {
        return restTemplate.postForObject(REST_URL_SHOPINGMALL + "/capital/findAllWithdrawApplys", baseDataReqDto, BaseMetaResDto.class);
    }

    /**
     * 审核分销资金提现申请
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("examineWithdrawApply")
    @ApiOperation(value = "审核分销资金提现申请")
    public BaseResDto examineWithdrawApply(@RequestBody BaseDataReqDto<WithdrawApply> baseDataReqDto) {
        return restTemplate.postForObject(REST_URL_SHOPINGMALL + "/capital/examineWithdrawApply", baseDataReqDto, BaseResDto.class);
    }


}
